package ecom.servlets;

import ecom.beans.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

public class AuthentServlet extends HttpServlet {

  /**
     * Serializable class uid.
     */
    private static final long serialVersionUID = 6893863749912962928L;


    /**
     * Called by the server (via the service method) to allow a servlet to
     * handle a GET request.
     * @param request an HttpServletRequest object that contains the request
     * the client has made of the servlet
     * @param response an HttpServletResponse object that contains the
     * response the servlet sends to the client
     * @throws IOException if an input or output error is detected when the
     * servlet handles the GET request
     * @throws ServletException if the request for the GET could not be handled
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    	AuthBeanWrapper auth = new AuthBeanWrapper();
    	
    	Boolean b =null;
		try {
			b = auth.authCustomer(request.getParameter("email"),request.getParameter("password"));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	if(b){
    	   		
    		
    		
    	    try {
    	    if(request.getParameter("pg").equals("compte"))
    	     getServletConfig().getServletContext().getRequestDispatcher("/identifie.jsp?compte="+request.getParameter("email")+"&pg=compte").forward(request,response);
    	    else if (request.getParameter("pg").equals("valid"))
    	    	getServletConfig().getServletContext().getRequestDispatcher("/identifie.jsp?compte="+request.getParameter("email")+"&pg=valid").forward(request,response);	
    	    else {
    	    	getServletConfig().getServletContext().getRequestDispatcher("/identifie.jsp?compte="+request.getParameter("email")+"&pg=rien").forward(request,response);	
    	    }
    	    } catch (ServletException e) {
    	      // TODO Auto-generated catch block
    	      e.printStackTrace();
    	    } catch (IOException e) {
    	      // TODO Auto-generated catch block
    	      e.printStackTrace();
    	    }
    	  }	
    	else{
    		try {
      	      getServletConfig().getServletContext().getRequestDispatcher("/identificationerror.jsp").forward(request,response);

      	    } catch (ServletException e) {
      	      // TODO Auto-generated catch block
      	      e.printStackTrace();
      	    } catch (IOException e) {
      	      // TODO Auto-generated catch block
      	      e.printStackTrace();
      	    }
      	  }	
    	}
    
    public void doPost (HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
    	doGet(request,response);
    
    }

}